package initialize

import (
	"doctor_srv/global"
	"doctor_srv/model"
	"fmt"
	"github.com/elastic/go-elasticsearch/v7"
	"github.com/winksai/utils/mysql"
	"github.com/winksai/utils/nacos"
	"github.com/winksai/utils/redis"
	"github.com/winksai/utils/viper"
	"log"
)

func init() {
	Viper()
	Nacos()
	Mysql()
	Redis()
	Elasticsearch()

}

func Viper() {
	viper.Viper("./dev.yaml")
}
func Nacos() {
	getNacos := viper.GetNacos()
	_, err := nacos.Nacos(getNacos.NameSpace, getNacos.Username, getNacos.Password, getNacos.Addr, getNacos.Port, getNacos.DataID, getNacos.Group)
	if err != nil {
		log.Println(err)
		return
	}
}

func Mysql() {
	config := nacos.GetConfig()
	mysqlConfig := config.Mysql
	db := mysql.InitMysql(mysqlConfig.User, mysqlConfig.Password, mysqlConfig.Host, mysqlConfig.Database, mysqlConfig.Port)
	db.AutoMigrate(&model.Consultation{})
	global.DB = db
}
func Redis() {
	config := nacos.GetConfig()
	redisConfig := config.Redis
	rdb := redis.InitRedis(redisConfig.Host, redisConfig.Password, 0)
	global.RedisClient = rdb
}

func Elasticsearch() {
	var err error
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://117.27.231.169:9200",
		},
		// ...
	}
	global.EsClient, err = elasticsearch.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	_, err = global.EsClient.Ping()
	if err != nil {
		log.Println(err)
		return
	}
	fmt.Println("es连接成功")
}
